<?php


namespace app\plugin\controller\draw_logic\com;


use app\admin\controller\com\Admin;
use app\common\controller\AppCommon;
use app\plugin\controller\draw_logic\model\PluginDrawLogic;
use app\service\PluginsService;
use think\Request;

class DrawLogic extends Admin
{
    public $tag = 'draw_logic';
    public $param = null;


    public $plugin = null;
    public $store_num = 0;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        if (!bs_p($this->tag)) {
            return $this->error('应用已关闭');
        }

        $this->param = input();
        $info = PluginsService::get_plugin_info($this->tag);
        if ($info['code'] == 0) {
            $this->plugin = $info['data'];
        }
        $this->ini_db();
        $this->assign('menu', $this->get_config('menu'));
    }

    private function get_config($key = '')
    {
        $config = require __DIR__ . '/config.php';
        if (!$key) {
            return $config;
        }
        return !empty($config[$key]) ? $config[$key] : [];

    }

    private function ini_db()
    {
        if (!AppCommon::query("SHOW TABLES LIKE '" . table_name('plugin_draw_logic') . "';")) {
            AppCommon::execute("CREATE TABLE `" . table_name('plugin_draw_logic') . "` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `key` varchar(10) NOT NULL DEFAULT '' COMMENT '唯一标识',
  `title` varchar(128) NOT NULL DEFAULT '' COMMENT '名称',
  `content` text COMMENT '逻辑内容',
  `add_time` int(10) DEFAULT '0',
  `up_time` int(10) DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `key` (`key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='逻辑记录';");

            //demo数据
            $demoArr = [
                ['key' => 'nzzjcv4sil', 'title' => 'demo-graph-td', 'content' => 'graph TD
A(test)-->B(step2)'],
                ['key' => '2whm1pxq4k', 'title' => 'demo-flowchart-tb', 'content' => 'flowchart TB
    A &amp; B--> C &amp; D'],
                ['key' => 'xkzghg2n8y', 'title' => 'demo-flowchart-if', 'content' => 'flowchart TD
    A[Start] --> B{Is it?}
    B -->|Yes| C[OK]
    C --> D[Rethink]
    D --> B
    B ---->|No| E[End]'],
                ['key' => '6rn0zos25k', 'title' => 'demo-sub二级模式', 'content' => 'flowchart TB
    c1-->a2
    subgraph one
    a1-->a2
    end
    subgraph two
    b1-->b2
    end
    subgraph three
    c1-->c2
    end'],
                ['key' => '5r2bg2zy31', 'title' => '稍微复杂点的demo', 'content' => 'flowchart LR
  subgraph TOP
    direction TB
    subgraph B1
        direction RL
        i1 -->f1
    end
    subgraph B2
        direction BT
        i2 -->f2
    end
  end
  A --> TOP --> B
  B1 --> B2'],
                ['key' => '25gutfek1c', 'title' => 'demo搞点颜色', 'content' => 'flowchart LR
    id1(Start)-->id2(Stop)
    style id1 fill:#f9f,stroke:#333,stroke-width:4px
    style id2 fill:#bbf,stroke:#f66,stroke-width:2px,color:#fff,stroke-dasharray: 5 5'],
                ['key' => 'kt6z7t5s91', 'title' => 'demo简单常规的', 'content' => 'flowchart LR
    A[Hard edge] -->|Link text| B(Round edge)
    B --> C{Decision}
    C -->|One| D[Result one]
    C -->|Two| E[Result two]'],
                ['key' => '8r3ac5m0jb', 'title' => 'demo数据类型', 'content' => 'flowchart LR
    id1[(Database)]'],
                ['key' => 'onpevyyfz2', 'title' => '圆形', 'content' => 'flowchart LR
    id1((This is the text in the circle))'],
            ];
            (new PluginDrawLogic())->saveAll($demoArr);
        }
    }

    protected function get_key($length = 10)
    {
        $str = '1234567890abcdefghijklmnopqrstuvwxyz';
        $key = '';
        for ($i = 0; $i < $length; $i++) {
            $key .= $str[mt_rand(0, strlen($str)) - 1];
        }
        if (AppCommon::data_get('plugin_draw_logic', ['key' => $key], 'id')) {
            return $this->get_key($length);
        }
        return $key;
    }
}